package cn.jy.machine.controller;

import cn.jy.common.index.MapChart;
import cn.jy.common.machine.domain.ExpressBoxes;
import cn.jy.common.machine.domain.ExpressVendingMachines;
import cn.jy.common.machine.domain.UseCountMap;
import cn.jy.common.machine.domain.dto.ExpressBoxUsageRecordsDto;
import cn.jy.common.machine.domain.dto.ExpressBoxesDto;
import cn.jy.common.machine.domain.dto.ExpressVendingMachinesDto;
import cn.jy.common.machine.domain.vo.ExpressBoxUsageRecordsVo;
import cn.jy.common.machine.domain.vo.ExpressBoxesVo;
import cn.jy.common.machine.domain.vo.ExpressVendingMachinesVo;
import cn.jy.machine.common.Result;
import cn.jy.machine.mapper.ExpressBoxUsageRecordsMapper;
import cn.jy.machine.mapper.ExpressBoxesMapper;
import cn.jy.machine.service.ExpressBoxesService;
import cn.jy.machine.service.ExpressVendingMachinesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/machine")
@Slf4j
public class ExpressVendingMachinesController {
    @Autowired
    private ExpressVendingMachinesService expressVendingMachinesService;
    @Autowired
    private ExpressBoxesMapper boxesMapper;
    @Autowired
    private ExpressBoxUsageRecordsMapper expressBoxUsageRecordsMapper;
    @Autowired
    private ExpressBoxesService expressBoxesService;

    /*
    * 柜机条件分页展示
    * */
    @PostMapping("/list")
    public Result list(@RequestBody ExpressVendingMachinesVo vo){
        vo.setOffset((vo.getPageNum() - 1) * vo.getPageSize());
        List<ExpressVendingMachinesDto> list = expressVendingMachinesService.list(vo);
        Long count =Long.valueOf(expressVendingMachinesService.count(vo));
        ArrayList<Integer> arrayList = new ArrayList<>();
        for (ExpressVendingMachinesDto dto : list) {
            arrayList.add(dto.getId());
        }
        List<UseCountMap> mapList =boxesMapper.selectBoxes(arrayList);
        for (ExpressVendingMachinesDto dto : list) {
            for (UseCountMap useCountMap : mapList) {
                if (useCountMap.getId()==dto.getId()){
                    dto.setUseCount(useCountMap.getUseCount());
                }
            }
        }
        return Result.publicSuccess(count,list);
    }
    /*
    * 开箱记录条件分页展示
    * */
    @PostMapping("/findBoxRecordList")
    public Result findBoxRecordList(@RequestBody ExpressBoxUsageRecordsVo vo){
        vo.setOffset((vo.getPageNum() - 1) * vo.getPageSize());
        List<ExpressBoxUsageRecordsDto> list = expressBoxesService.list(vo);
        Long count =Long.valueOf(expressBoxesService.count(vo));
        return Result.publicSuccess(count,list);
    }

    /*
    * 箱格条件展示
    * */
    @PostMapping("/getBoxList")
    public Result getBoxList(@RequestBody ExpressBoxesVo vo){
        List<ExpressBoxesDto> list = boxesMapper.findBoxList(vo);
        ArrayList<Integer> ids = new ArrayList<>();
        for (ExpressBoxesDto dto : list) {
            ids.add(dto.getId());
        }
        if (ids != null && !ids.isEmpty()){
            List<UseCountMap> mapList = expressBoxUsageRecordsMapper.selectBoxUsageRecords(ids);
            List<UseCountMap> mapList1 = expressBoxUsageRecordsMapper.findOpenDateTime(ids);
            for (ExpressBoxesDto dto : list) {
                for (UseCountMap map : mapList) {
                    if (map.getId().equals(dto.getId())){
                        dto.setOpenCount(map.getUseCount());
                    }
                }
                for (UseCountMap map : mapList1) {
                    if (map.getId().equals(dto.getId())){
                        dto.setOpenDateTime(map.getOpenDateTime());
                    }
                }
            }
        }
        return Result.success(list);
    }

    /*
    * 修改柜机状态
    * */
    @PostMapping("/updateStatus")
    public Result updateStatus(@RequestBody ExpressVendingMachines machines){
        expressVendingMachinesService.updateStatus(machines);
        return Result.success();
    }
    /*
    * 修改箱格信息
    * */
    @PostMapping("/updateBox")
    public Result updateBox(@RequestBody ExpressBoxes boxes){
        expressBoxesService.updateBox(boxes);
        return Result.success();
    }

    /*
    * 柜机添加
    * */
    @PostMapping("/addMachine")
    public Result addMachine(@RequestBody ExpressVendingMachinesDto dto){
        expressVendingMachinesService.addMachine(dto);
        return Result.success();
    }

    /*
    * 柜机批量添加
    * */
    @PostMapping("/save")
    public Result save(@RequestBody List<ExpressVendingMachines> list){
        expressVendingMachinesService.save(list);
        return Result.success();
    }

    /*
    * 根据id查找详细信息
    * */
    @GetMapping("/findMachineById/{id}")
    public Result findMachineById(@PathVariable("id") Integer id){
        ExpressVendingMachinesDto dto=expressVendingMachinesService.findMachineById(id);
        Integer a=0;
        List<ExpressBoxes> list=boxesMapper.findBoxesById(dto.getId());
        for (ExpressBoxes boxes : list) {
            if (boxes.getIsStatus()==1){
                a++;
            }
        }
        if (a==0){
            return Result.success(dto);
        }else {
            dto.setUseCount(a);
            return Result.success(dto);
        }
    }


    /*
    * 根据id查找详细信息
    * */
    @GetMapping("/findBoxById/{id}")
    public Result findBoxById(@PathVariable("id") Integer id){
        ExpressBoxesDto dto=expressBoxesService.findBoxById(id);
        return Result.success(dto);
    }


    @GetMapping("/getNewOpenNum")
    public Result getNewOpenNum(){
        Integer num=expressBoxesService.newOpenNum();
        return Result.success(num);
    }

    @GetMapping("/getMapChart")
    public Result getMapChart(){
        List<MapChart> list=expressVendingMachinesService.getMapChart();
        return Result.success(list);
    }
}
